package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description="讲师管理")
@CrossOrigin  //解决跨域问题
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result findTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items",list);
    }

    //逻辑删除讲师信息
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public Result removeTeacger(@ApiParam(name = "id", value = "讲师ID", required = true)
                                    @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag)
            return Result.ok();
        else
            return Result.error();
    }

    //分页查询讲师方法
    @GetMapping("/pageTeacher/{current}/{limit}")
    public Result pageTeacher(@PathVariable Long current,
                              @PathVariable Long limit){

        //创建Page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现分页查询
        //调用方法的时候,底层会把分页的所有数据封装到pageTeacher对象中
        teacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//当前页数据集合
        return Result.ok().data("total",total).data("rows",records);
    }


    //条件查询带分页
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable Long current,
                                       @PathVariable Long limit,
                                       @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current,limit);
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        if (!StringUtils.isEmpty(level))
            wrapper.eq("level", level);
        if (!StringUtils.isEmpty(begin))
            wrapper.ge("gmt_create", begin);
        if (!StringUtils.isEmpty(end))
            wrapper.le("gmt_create", end);
        //排序
        wrapper.orderByDesc("gmt_create");

        teacherService.page(page,wrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save)
            return Result.ok();
        else
            return Result.error();
    }


    //根据讲师id查询讲师信息
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return Result.ok().data("teacher",eduTeacher);
    }
    //讲师修改功能
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
//        if (flag)
//            return Result.ok();
//        else
//            return Result.error();
        return flag? Result.ok():Result.error();
    }



}
